/**
 * 
 */
package galacticthrone.screen;

import galacticthrone.main.GTGame;
import galacticthrone.properties.VideoOptions;

import javax.media.opengl.GL;

import javagame.core.io.Screen.ScreenRenderer;
import javagame.core.io.video.texture.Texture;

// TODO Javadoc
/**
 * <br>
 *
 * @author Jaco van der Westhuizen
 */
public class MouseCursor implements ScreenRenderer
{
    private final String texName;
    private final float size;
    private final float vx0;
    private final float vy0;
    private final float vx1;
    private final float vy1;

    protected Texture tex;
    protected float scaleX;
    protected float scaleY;
    protected float transX;
    protected float transY;
    protected int x;
    protected int y;
    protected boolean isVisible;

    public MouseCursor(String texName, int size, int hotSpotX, int hotSpotY)
    {
        this.texName = texName;
        this.size = size * VideoOptions.yRes / 600.0f;
        this.vx0 = (hotSpotX + .5f) / size;
        this.vy0 = (hotSpotY + .5f) / size;
        this.vx1 = vx0 - 1;
        this.vy1 = vy0 - 1;
    }

    /* (non-Javadoc)
     * @see javagame.core.io.Screen.ScreenRenderer#loadAssets(javax.media.opengl.GL, boolean)
     */
    @Override
    public void loadAssets(GL gl, boolean isInitial)
    {
        if (isInitial)
            tex = GTGame.getTextureManager().grab(texName);
    }

    /* (non-Javadoc)
     * @see javagame.core.io.Screen.ScreenRenderer#onScreenReshape(int, int)
     */
    @Override
    public void onScreenReshape(int width, int height)
    {
        this.scaleY = -2.0f * size / height;
        
        // TODO Won't work right when window ratio mismatches res ratio initially
        this.scaleX = -2.0f * size  / width / VideoOptions.screenAspectRatio / VideoOptions.yRes * VideoOptions.xRes;
        this.transX = 2.0f / width;
        this.transY = 2.0f / height;
    }

    /* (non-Javadoc)
     * @see javagame.core.io.Screen.ScreenRenderer#renderForDisplay(javax.media.opengl.GL)
     */
    @Override
    public void renderForDisplay(GL gl)
    {
        if (!isVisible)
            return;

        gl.glLoadIdentity();
        gl.glTranslatef(x * transX - 1, 1 - y * transY, 0);
        gl.glScalef(scaleX, scaleY, 1);

        tex.activate(gl);

        gl.glBegin(GL.GL_TRIANGLE_FAN);

        gl.glColor4f(1, 1, 1, 1);
        gl.glTexCoord2f(0, 0);
        gl.glVertex2f(vx0, vy0);
        gl.glTexCoord2f(1, 0);
        gl.glVertex2f(vx1, vy0);
        gl.glTexCoord2f(1, 1);
        gl.glVertex2f(vx1, vy1);
        gl.glTexCoord2f(0, 1);
        gl.glVertex2f(vx0, vy1);

        gl.glEnd();
        gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE_MINUS_SRC_ALPHA);
    }

    /* (non-Javadoc)
     * @see javagame.core.io.Screen.ScreenRenderer#renderForPicking(javax.media.opengl.GL)
     */
    @Override
    public void renderForPicking(GL gl)
    {
    }

    /* (non-Javadoc)
     * @see javagame.core.io.Screen.ScreenRenderer#unloadAssets(javax.media.opengl.GL, boolean)
     */
    @Override
    public void unloadAssets(GL gl, boolean isFinal)
    {
        if (isFinal)
        {
            GTGame.getTextureManager().release(gl, tex);
            tex = null;
        }
    }

    public void moveTo(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    /**
     * @param isVisible
     */
    public void setVisible(boolean isVisible)
    {
        this.isVisible = true;
    }
}
